package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.liveobject.resolver.LongGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : gao
 * @since: 2022/10/25  19:55
 */

@RestController
public class CartItemController {

    @Autowired
    ICartService cartService;


    @PostMapping("/shopping/carts")
    public ResponseData addToCarts(@RequestBody CartRequest cartRequest) {
        cartService.addToCart(cartRequest);
        return new ResponseUtil().setData(null, null);
    }



    /**
    * @Author Yi
    * @Description 更新购物车中的商品
    * @Date 2022/10/26 17:34
    */
    @PutMapping("/shopping/carts")
    public ResponseData updateCartNum(@RequestBody CartRequest cartRequest) {
        cartService.updateCartNum(cartRequest);
        return new ResponseUtil().setData(null, null);

    }

    /**
     * 删除购物车中某一件商品
     *
     * @return {@link null}
     * @author gao
     * @since 2022/10/25 20:06
     */
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData DeleteCartProduct(@PathVariable("uid") Long uid,
                                          @PathVariable("pid") Long pid) {
        cartService.deleteCartItem(uid, pid);
        ResponseData responseData = new ResponseUtil().setData(null, null);
        long times = System.currentTimeMillis();
        responseData.setTimestamp(times);
        return responseData;
    }


    /**
     * 删除购物车中选中的商品
     *
     * @return {@link null}
     * @author gao
     * @since 2022/10/26 11:03
     */
    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData DeleteCartSelectProduct(@PathVariable("uid") Long uid) {
        cartService.deleteCheckedItem(uid);
        ResponseData responseData = new ResponseUtil().setData(null, null);
        long times = System.currentTimeMillis();
        responseData.setTimestamp(times);
        return responseData;
    }

    @PutMapping("/shopping/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        cartService.checkAllCartItem(checkAllItemRequest);
        ResponseData responseData = new ResponseUtil().setData(null, null);
        long times = System.currentTimeMillis();
        responseData.setTimestamp(times);
        return responseData;
    }
}
